package application;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.usb.*;

/*
 * Uses USB4Java to locate user's camera
 * Should find USB device attached and locate ALL images/videos
 * into a list. The media will be stored in a folder then
 * the media removed from the camera.
 */

public class Dump {
	private static String[] usbs = new String[10];
	private static void dump(UsbDevice device) throws UnsupportedEncodingException, UsbDisconnectedException, UsbException {
		UsbDeviceDescriptor desc = device.getUsbDeviceDescriptor();
		System.out.format("%04x:%04x%n", desc.idVendor() & 0xffff,
				desc.idProduct() & 0xffff);
		if (device.isUsbHub()) {
			UsbHub hub = (UsbHub) device;
			int i =0;
			for (UsbDevice child : (List<UsbDevice>) hub.getAttachedUsbDevices()) {
				usbs[i] = child.getProductString();
				
				i++;
				dump(child);
			}
		}
	}

	public static void main(String[] args) throws UsbException, UnsupportedEncodingException, UsbDisconnectedException {
		UsbServices services = UsbHostManager.getUsbServices();
		UsbHub rootHub = services.getRootUsbHub();
		dump(rootHub);
	}

	public void printUSB() throws SecurityException, UsbException, UnsupportedEncodingException, UsbDisconnectedException{
		
		UsbServices services = UsbHostManager.getUsbServices();
		UsbHub rootHub = services.getRootUsbHub();
		dump(rootHub);
		
		
		for (String s : usbs){
			System.out.println(s);
		}
	}
	public UsbDevice findDevice(UsbHub hub, short vendorId, short productId) {
		for (UsbDevice device : (List<UsbDevice>) hub.getAttachedUsbDevices()) {
			UsbDeviceDescriptor desc = device.getUsbDeviceDescriptor();
			if (desc.idVendor() == vendorId && desc.idProduct() == productId)
				return device;
			if (device.isUsbHub()) {
				device = findDevice((UsbHub) device, vendorId, productId);
				if (device != null)
					return device;
			}
		}
		return null;
	}
}